﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace Easydp.Models
{
    public class RouteManager
    {
        public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 
        public int Update(Entities.Route model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Route set ");
            strSql.Append("TrainNumber=@TrainNumber,");
            strSql.Append("TrainType=@TrainType,");
            strSql.Append("Countimes=@Countimes,");
            strSql.Append("CountMile=@CountMile,");
            strSql.Append("StartStation=@StartStation,");
            strSql.Append("EndStation=@EndStation,");
            strSql.Append("CreateOn=@CreateOn");
            strSql.Append(" where RouteID=@RouteID ");
           

            using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@RouteID", model.RouteID);
                updation.AddParameter("@TrainNumber", model.TrainNumber);
                updation.AddParameter("@TrainType", model.TrainType);
                updation.AddParameter("@Countimes", model.Countimes);
                updation.AddParameter("@CountMile", model.CountMile);
                updation.AddParameter("@StartStation", model.StartStation);
                updation.AddParameter("@EndStation", model.EndStation);
                updation.AddParameter("@CreateOn", model.CreateOn);
               
                return updation.ExecuteNonQuery();

            }
            
        }
        /// <summary>
        /// 得到model一个对象实体
        /// </summary>
        /// 
        public Entities.Route GetModel(int RouteID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RouteID,TrainNumber,TrainType,Countimes,CountMile,StartStation,EndStation,CreateOn from Route ");
            strSql.Append(" where RouteID=@RouteID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RouteID", SqlDbType.Int,4)};
            parameters[0].Value = RouteID;

            Entities.Route model = new Entities.Route();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RouteID"].ToString() != "")
                {
                    model.RouteID = int.Parse(ds.Tables[0].Rows[0]["RouteID"].ToString());
                }
                model.TrainNumber = ds.Tables[0].Rows[0]["TrainNumber"].ToString();
                model.TrainType = ds.Tables[0].Rows[0]["TrainType"].ToString();
                model.Countimes = ds.Tables[0].Rows[0]["Countimes"].ToString();
                if (ds.Tables[0].Rows[0]["CountMile"].ToString() != "")
                {
                    model.CountMile = int.Parse(ds.Tables[0].Rows[0]["CountMile"].ToString());
                }
                model.StartStation = ds.Tables[0].Rows[0]["StartStation"].ToString();
                model.EndStation = ds.Tables[0].Rows[0]["EndStation"].ToString();
                if (ds.Tables[0].Rows[0]["CreateOn"].ToString() != "")
                {
                    model.CreateOn = DateTime.Parse(ds.Tables[0].Rows[0]["CreateOn"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }




        /// <summary>
        /// 获得dataset数据列表
        /// </summary>
        public DataSet GetDataset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RouteID,TrainNumber,TrainType,Countimes,CountMile,StartStation,EndStation,CreateOn ");
            strSql.Append(" FROM Route ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到list列表
        /// </summary>
        /// 
        public List<Entities.Route> GetList(int RouteID)
        {
            List<Entities.Route> list = new List<Easydp.Models.Entities.Route>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   RouteID,TrainNumber,TrainType,Countimes,CountMile,StartStation,EndStation,CreateOn from Route ");
            strSql.Append(" where RouteID=@RouteID ");
            using (SqlText selectlist = new SqlText(strSql.ToString()))
            {
                selectlist.AddParameter("@RouteID", RouteID);
                while (selectlist.Read())
                {
                    Entities.Route model = new Easydp.Models.Entities.Route();
                    if (selectlist["RouteID"].ToString() != "")
                    {
                        model.RouteID = int.Parse(selectlist["RouteID"].ToString());
                    }
                    model.TrainNumber = selectlist["TrainNumber"].ToString();
                    model.TrainType = selectlist["TrainType"].ToString();
                    model.Countimes = selectlist["Countimes"].ToString();
                    if (selectlist["CountMile"].ToString() != "")
                    {
                        model.CountMile = int.Parse(selectlist["CountMile"].ToString());
                    }
                    model.StartStation = selectlist["StartStation"].ToString();
                    model.EndStation = selectlist["EndStation"].ToString();
                    if (selectlist["CreateOn"].ToString() != "")
                    {
                        model.CreateOn = DateTime.Parse(selectlist["CreateOn"].ToString());
                    }
                    list.Add(model);
                }
            }
            return list;
        }
    }
}
